<template>
  <div class="settWrap">
    <div class="wrapCont">
      <div class="header">
        <ul class="clearfix">
          <li v-for="item in statusList" :ref="item.class" @click='onStatus(item.status)' :key="item.status">
            <p :class="{'active': statusActive == item.status}">{{item.txt}}</p>
            <i v-show="statusActive == item.status"></i>
          </li>
        </ul>
      </div>
      <swiper :options="swiperOption" ref="mySwiper">
        <swiper-slide  v-for="item in statusList" :key="item.status" >
            <div class="main-body" ref="wrapper" :style="{'height':wrapperHeight+'px'}">
              <mt-loadmore :top-method="loadTop" @top-status-change="handleTopChange" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" :bottomPullText='bottomText' :ref="item.loadmore">
                <mt-swiper-slide-cont :child-list="list[item.status]"></mt-swiper-slide-cont>
                <div slot="top" class="mint-loadmore-top">
                    <span v-show="topStatus !== 'loading'" :class="{ 'rotate': topStatus === 'drop' }">↓</span>
                    <span v-show="topStatus === 'loading'">正在刷新...</span>
                </div>
              </mt-loadmore>
            </div>

        </swiper-slide>
      </swiper>
      <!-- <div ref="abc" style="height:120px">1111111111</div> -->
      <m-tabbar ref="footer" :child-msg="msg"></m-tabbar>
    </div>
  </div>
</template>
<script type="text/ecmascript-6">
  import Vue from 'vue'
  import ApiService from '@/services/API-servies'
  import {
    Tabbar,
    TabItem,
    Cell,
    TabContainer,
    TabContainerItem,
    Button,
    InfiniteScroll,
    Indicator,
    Spinner,
    Popup,
    DatetimePicker,
    Loadmore
  } from 'mint-ui';
  import MTabbar from '../foot/Tabbarfoot'
  import CountDown from '../public/public.vue'
  import { swiper, swiperSlide } from 'vue-awesome-swiper'
  import 'swiper/dist/css/swiper.css'
  import MtSwiperSlideCont from './MtSwiperSlideCont'

  Vue.use(InfiniteScroll);
  export default {
    data() {
      return {
        statusList: [{
          status: '0',
          txt: '全部结算单',
          loadmore:'loadmore'
        }, {
          status: '35',
          txt: '待开票',
          loadmore:'loadmore35'
        }, {
          status: '65',
          txt: '已收款',
          loadmore:'loadmore65'
        }, {
          status: '50',
          txt: '结算完成',
          loadmore:'loadmore50'
        }],
        searchStatusActive: '',
        searchStatus: [{
          status: '',
          txt: '全部',
        }, {
          status: '',
          txt: '待开票',
        }, {
          status: '',
          txt: '结算完成',
        }],
        statusActive: '',
        loadList: [],
        session: '',
        setUserInfo2: '',
        timeTit: '',
        bidTime: '',
        bidTimeList: [],
        msg: 'settlement',
        list: {
          '0':[],
          '35':[],
          '65':[],
          '50':[],
        },
        sortFlog: true,//默认是按照正序价格排列
        priceChecked: 'all',//默认选中的价格范围
        busy: true,//默认允许滚动执行loadMore函数
        pageNo: 1,//默认分页为第一页
        pageSize: 10,//默认每页显示8条数据
        flag: false,//默认没有分页
        showLoading: true,
        showALlLoad: false,
        searchPopup: false,
        pickerValue: new Date(),
        timeType:'',
        beginTime: '',
        endTime: '',
        invoiceNum:"",
        voucherNum:"",
        settleId:"",
        allLoaded:false,
        topStatus:'',
        scrollMode:'auto',
        bottomText:'上拉加载更多...',
        userType: '30',
        wrapperHeight:0,
        page:'',
        index:'',
        swiperOption: {
          // NotNextTick is a component's own property, and if notNextTick is set to true, the component will not instantiate the swiper through NextTick, which means you can get the swiper object the first time (if you need to use the get swiper object to do what Things, then this property must be true)
          // notNextTick是一个组件自有属性，如果notNextTick设置为true，组件则不会通过NextTick来实例化swiper，也就意味着你可以在第一时间获取到swiper对象，假如你需要刚加载遍使用获取swiper对象来做什么事，那么这个属性一定要是true
          notNextTick: true,
          autoplay: 3000,
          grabCursor : true,
          setWrapperSize :true,
          autoHeight: true,
          pagination : '.swiper-pagination',
          mousewheelControl : true,
          observeParents:true,
          loop: true,
          pagination: '.swiper-pagination',
           debugger: true,
          onSlideChangeEnd: swiper => {
            console.log('onSlideChangeEnd', swiper.realIndex)
          }
        }
      }
    },
    created() {
      window.clickSearch = this.clickSearch;
      if(localStorage.getItem('localSettleStatus')){
        this.statusActive = localStorage.getItem('localSettleStatus');
      };
      
    },
    mounted() {
      setTimeout(() => {
        if (this.$isBrower.isAndroid) {
          this.getAndWay();
          this.setAndTitleType();
        } else if (this.$isBrower.isIos) {
          this.setIosTitleType();
          this.iosSearch();
        }
      }, 0)
      for(let i in this.statusList){
        this.loadListFun(this.statusList[i].status);
      };
    },
    computed: {
      swiper() {
        return this.$refs.mySwiper.swiper
      }
    },
    methods: {
      loadTop() {
        //    this.$refs.loadmore.onTopLoaded();
            this.pageNo = 1;
            this.loadListFun();
            // this.$refs.loadmore.onTopLoaded();
        },
        loadBottom() {
          // this.allLoaded = true;// 若数据已全部获取完毕
            // this.$refs.loadmore.onBottomLoaded();
            this.pageNo++;
            this.loadListFun(true);
            
        },
        handleTopChange(status) {
          this.topStatus = status;
        },
      loadListFun(status,flag) {
        this.showLoading = true;
        this.showALlLoad = false;
        let params = {
          pageSize: this.pageSize,
          pageNo: this.pageNo,
          status:status
        }
        if (this.userType == 30) {
          let _this = this;
          if(params.status == 0){
            params.status = ''
          }
          ApiService.transport.load(params).then((res) => {
            this.showLoading = true;
            if (flag && this.userType == 30) {//判断是否通过分页
              this.list[status] = this.list[status].concat(res.data.list);//分页数据追加到这个list里面
              if (res.data.list.length == 0) {//判断数是否加载完成，就让数据截停
                this.busy = true;
                this.showLoading = false;
                this.showALlLoad = true;
              } else {
                this.busy = false;
              }
              this.$refs['loadmore'+status].onBottomLoaded();
            } else if (this.userType == 30) {
              this.list[status] = [];
              this.list[status] = res.data.list;
              this.busy = false;
              if (res.data.list.length == 0 || res.data.list.length < 10) {//判断数是否加载完成，就让数据截停
                this.busy = true;
                this.showLoading = false;
                this.showALlLoad = true;
              }
              // console.log(_this.$refs['loadmore'+status])
              // _this.$refs['loadmore'+status].onTopLoaded();
    
              // console.log(this.$refs.abc.offsetWidth)
                //  console.log(this.$refs.footer.$el.clientHeight,this.$refs.wrapper.offsetTop)
                //  console.log(this.$refs.footer.$el.clientHeight,this.$refs.wrapper.offsetTop)
              this.wrapperHeight = document.documentElement.clientHeight - 120;
              // this.wrapperHeight = document.documentElement.clientHeight - (this.$refs['loadmore'+status].offsetTop + this.$refs.footer.$el.clientHeight)/2;
            }
          });
        } else if (this.userType == 40) {
          ApiService.driver.load(params).then((res) => {
            this.showLoading = true;
            if (flag && this.userType == 40) {//判断是否通过分页
              this.list = this.list.concat(res.data.list);//分页数据追加到这个list里面
              if (res.data.list.length == 0) {//判断数是否加载完成，就让数据截停
                this.busy = true;
                this.showLoading = false;
                this.showALlLoad = true;
              } else {
                this.busy = false;
              }
            } else if (this.userType == 40) {
              this.list = res.data.list;
              this.busy = false;
              if (res.data.list.length == 0 || res.data.list.length < 10) {//判断数是否加载完成，就让数据截停
                this.busy = true;
                this.showLoading = false;
                this.showALlLoad = true;
              }
            }
          });
        }
      },
    //   loadMore: function () {
    //     this.busy = true;
    //     setTimeout(() => {
    //       this.pageNo++;
    //       this.loadListFun(true);
    //     }, 500)
    //   },
      onStatus(re) {
        this.statusActive = re;
        localStorage.setItem('localSettleStatus',this.statusActive);
        this.list = [];
        this.pageNo = 1;
        this.loadListFun();
      },
      tosearch() {
        this.pageNo = 1;
        this.list = [];
        this.loadListFun();
        this.searchPopup = false;
      },
      toReset() {
        this.beginTime = '';
        this.endTime = '';
        this.invoiceNum = '';
        this.voucherNum = '';
        this.settleId = '';
      },
      searchOnStatus(index) { 
        this.searchStatusActive = index;
      },
      toDetail(settleId) {
        this.$router.push({path: 'settlementDetail', query: {settleId: settleId}})
      },
      toInvoicing(settleId, _status) {
        this.$router.push({path: 'invoicing', query: {settleId: settleId}})
      },
      getAndWay() {
          app.setWaybill();
      },
      setAndTitleType() {
          app.setTitleType({
            isReturn: false,
            isSearch: true
          });
      },
      isShow(val) {
        if (val == 35 || val == 60) {
          return true;
        }
        return false;
      },
      arrived(transBillId) {
        ApiService.transport.arrived({
          transBillId: transBillId
        }).then((res) => {
          if (res.meta.retCode === '0') {
            return;
          }
        });
      },
      openPicker(type) {
        this.timeType = type;
        this.$refs.startPicker.open();
      },
      start(value) {
        let start = this.$moment(value).format("YYYY-MM-DD HH:mm");
        if(this.timeType == 0){
          this.beginTime = start;
        }else if(this.timeType == 1){
          this.endTime = start;
        }
      },
      // andriod 搜索方法
      clickSearch() {
        this.searchPopup = true;
      },
      //ios 搜索方法
      iosSearch() {
        this.$bridge.registerhandler('searchPopup', (data) => {
          this.searchPopup = true;
        })
      },
      //ios 设置头部信息
      setIosTitleType(){
        let params = {
          isReturn: false,
          isSearch: true,
          isTitleSegment:false
        };
        this.$bridge.callhandler('needBarButton', params, (data) => {})
      }
    },
    filters: {
      status(val) {
        if (val == 30) {
          return '待竞价'
        } else if (val == 40) {
          return '参与竞价'
        }
      },
      city(val) {
        var index = val.indexOf('-') + 1;
        return val.substring(index)
      },
      settleStatusBtn(val) {
        if (val == 30) {
          return '发票上传'
        } else if (val == 50) {
          return '发票上传'
        } else if (val == 35 || val == 40) {
          return '发票上传'
        }
      }
    },
    components: {
      MtTabbar: Tabbar,
      MtTabItem: TabItem,
      mtCell: Cell,
      MtTabContainer: TabContainer,
      MtTabContainerItem: TabContainerItem,
      MTabbar,
      MtButton: Button,
      CountDown,
      MtSpinner: Spinner,
      MtPopup: Popup,
      MtDatetimePicker: DatetimePicker,
      MtLoadmore: Loadmore,
      swiper,
      swiperSlide,
      MtSwiperSlideCont
    }
  }
</script>

<style lang="less" rel="stylesheet/less">
  @import "~common/less/variable";

  .settWrap {
    .wrapCont {
      padding: 0px 0 120px;
      .main-body{
        overflow: scroll;
        -webkit-overflow-scrolling:touch;
        margin-top: 120px;
      }
      .header {
        position: fixed;
        top: 0;
        width: 100%;
        z-index: 99;
        background: #fff;
        padding-top: 20px;
        margin-bottom: 10px;
        box-shadow: 0px 4px 10px 0px rgba(183,186,199,0.5);
        ul {
          li {
            float: left;
            width: 25%;
            text-align: center;
            &.driverLi {
              width: 33.33%;
            }
            p {
              font-size: 28px;
              color: #5C6063;
              margin-bottom: 10px;
              &.active {
                color: @blue
              }
            }
            i {
              display: block;
              margin: 0 auto;
              width: 60%;
              height: 6px;
              background: @blue;
            }
          }
        }
      }
      .contant{
        margin-top: 120px;
      }
      .offerList {
        font-size: 28px;
        padding: 10px 20px;
        .singerBox {
          background: #fff;
          padding: 30px;
          padding-bottom: 10px;
          border-radius: 16px;
          box-shadow: 0px 4px 10px 0px rgba(183,186,199,0.5);
          .boxTitle {
            margin-bottom: 12px;
            .left {
              line-height: 36px;
              color: #999;
              &.name{
                // color: #333;
              }
            }
            .right {
              background: #5395FF;
              color: #fff;
              padding: 8px 12px;
              border-radius: 200px;
              font-size: 24px;
              font-weight: 400;
              line-height: 28px
            }
            .date{
              color: #999;
              line-height: 36px;
            }
          }
          .address {
            font-size: 32px;
            font-weight: 800;
            position: relative;
            & span {
              font-weight: 500
            }
            .iconTo {
              display: inline-block;
              width: 60px;
              position: absolute;
              left: 50%;
              background: url(../../common/imgs/iconTo.png) no-repeat center;
              background-size: 100%;
              transform: translateX(-50px)
            }
            .add {
              width: 267px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }
          .info {
            font-size: 26px;
            color: #666;
            margin-bottom: 6px;
            line-height: 40px;
            .down {
              margin-top: 10px;
              span {
                color: #FF0505
              }
              .fr i {
                color: #FF0505;
                font-weight: 400;
              }
            }
          }
          .border {
            border-top: 3px dashed #E8E7E8;
            height: 3px;
            margin: 20px 0;
            position: relative;
            .halfLe{
              display: block;
              position: absolute;
              left: -44px;
              top:-20px;
              width: 40px;
              height: 40px;
              background: url(../../common/imgs/harfLe.png) no-repeat center center;
              background-size: contain;
            }
            .halfRi{
              display: block;
              position: absolute;
              right: -44px;
              top:-20px;
              width: 40px;
              height: 40px;
              background: url(../../common/imgs/harfRi.png) no-repeat center center;
              background-size: contain;
            }
          }
          .dispatchBtnWrap {
            text-align: right;
            .fl {
              color: @blue;
              font-size: 26px;
              line-height: 60px;
            }
            .btn {
              width: 180px;
              font-size: 28px;
              line-height: 30px;
              height: 52px;
              border-radius: 8px;
              background: @blue
            }
          }
        }
      }
      .more_loading {
        text-align: center;
        .mint-spinner-snake {
          display: inline-block;
          vertical-align: middle;
          margin-right: 10px
        }
      }
      // .loadmoreWrap{
      //   padding-top: 120px;
      // }
    }
    .searchWrap {
      width: 660px;
      text-align: center;
      .head {
        h3 {
          color: #F9F3F3;
          line-height: 108px;
          font-size: 36px;
          background: @blue;
        }
        span {
          position: absolute;
          top: 26px;
          right: 20px;
          width: 56px;
          height: 56px;
          line-height: 48px;
          font-size: 56px;
          color: #F9F3F3
        }
      }
      .searCont {
        .list {
          margin: 0 30px;
          text-align: left;
          p {
            font-size: 28px;
            color: #333;
            font-weight: 400;
          }
          .iptDiv {
            background: #F0F2F5;
            padding: 14px;
            line-height: 10px;
            input {
              color: #333;
              font-size: 26px;
              height: 56px;
              width: 99%;
            }
            &.timeGroup {
              span {
                color: #C1C1C1;
                font-weight: 800
              }
              input {
                width: 254px;
              }
            }
          }
          .status {
            li {
              position: relative;
              float: left;
              color: #999;
              font-size: 26px;
              padding: 4px 30px;
              background: #F0F2F5;
              margin-right: 18px;
              margin-bottom: 20px;
              display: inline-block;
              border-radius: 8px;
              border: 2px solid #F0F2F5;
              &:nth-child(4), &:nth-child(7) {
                margin-right: 0;
              }
              &.active {
                border: 2px solid #2F7AF3;
                color: #2F7AF3
              }
              &.active:after {
                content: "";
                position: absolute;
                bottom: 0;
                right: 0;
                width: 36px;
                height: 32px;
                background: url(../../common/imgs/check.png) no-repeat 0 0;
                background-size: 100%;
              }
            }
          }
        }
      }
      .foot {
        border-top: 2px solid #D8D8D8;
        padding: 10px 20px 0;
        margin: 0;
        a {
          display: block;
          width: 300px;
          height: 80px;
          background: @blue;
          color: #fff;
          font-weight: 400;
          font-size: 32px;
          line-height: 80px;
          text-align: center;
          border-radius: 8px;
          margin: 40px auto;
        }
      }
    }
    .clickH5Btn2 {
      margin-top: 20px
    }
  }
</style>
